#!/bin/bash
# $Id$
# /*!
#	@header logouthook.sh
#	@abstract Captures settins and files in the denisonmac account for use in
#	the default user experience upon first login.
#	@discussion This script is meant to run as a LogoutHook for the loginwindow
#	process. The basic process is:
#	1. Check to see if the person logging out is denisonmac, and if not, exit clealy
#	2. Check to see if there is a backup of the English.lproj folder, and delete it if so.
#	3. Create a fresh backup of the English.lproj folder and delete it.
#	4. Empty the trash, cache and keychains from /Users/denisonmac
#	5. Copy all of /Users/denisonmac into English.lproj
#
#	This essentially allows anyone how knows the denisonmac credentials to
#	modify the default template for the system by simply logging in, making the
#	changes and then logging out. Normally this is used during the image
#	building process.
#
#	@deprecated This method of template modification is still fully supported,
#	but the plan is to move towards capturing the default template in a package
#	installer. In doing so, we will get one step closer to a completely
#	modular image process.
# */

### Debug/testing sanity check ###
if [[ $# < 1 ]]
then
	echo "No user specified! Logouthook exiting."
	exit 1
fi

if [[ "${1}" == "denisonmac" ]]
then
	username="${1}"
	echo "'${1}' is logging out. Default template will be updated."
else
	echo "'${1}' is not denisonmac. Default template will not be updated."
	exit 0
fi

previousTemplate="/System/Library/User Template/English.lproj.previous"
template="/System/Library/User Template/English.lproj"
denisonmacHome="/Users/denisonmac"

if [ -d "${denisonmacHome}" ]
then

	# If there is already a previous template, delete it before backing up the current one.
	if [ -d "${previousTemplate}" ]
	then
		/bin/rm -rf "${previousTemplate}"
	else
		echo "${previousTemplate} doesn't exist, nothing to jettison."
	fi

	# Only create a previous version of the template if it exists.
	# It should, but better safe than sorry.
	if [ -d "${template}" ]
	then
		/bin/mv "${template}" "${previousTemplate}"
	else
		echo "Backing up ${template} to ${previousTemplate}"
	fi

	echo "Removing caches, logs, keychains, downloads and trash from ${denisonmacHome}"
	/bin/rm -rfv "${denisonmacHome}/.Trash/"*
	/bin/rm -rfv "${denisonmacHome}/Downloads/"*
	/bin/rm -rfv "${denisonmacHome}/Library/Caches/"*
	/bin/rm -rfv "${denisonmacHome}/Library/Logs/"*
	/bin/rm -rfv "${denisonmacHome}/Library/Keychains"
	/bin/rm -rfv "${denisonmacHome}/Library/Cookies/"*
	/bin/rm -rfv "${denisonmacHome}/Library/Safari/Databases/"*
	/bin/rm -rfv "${denisonmacHome}/Library/Safari/Downloads.plist"
	/bin/rm -rfv "${denisonmacHome}/Library/Safari/History.plist"
	/bin/rm -rfv "${denisonmacHome}/Library/Safari/HistoryIndex.sk"
	/bin/rm -rfv "${denisonmacHome}/Library/Safari/LastSession.plist"
	/bin/rm -rfv "${denisonmacHome}/Library/Safari/WebpageIcons.db"

	# Finally, we ditto the denisonmac account into the system-wide template
	# location.
	echo "Copying ${denisonmacHome} to ${template}"
	/usr/bin/ditto -rsrc "${denisonmacHome}" "${template}"

	if [ $? -eq 0 ]
	then
		echo "Repairing permissions on new ${template}"
		/usr/sbin/chown -R root:wheel "${template}"
		/bin/chmod -R 700 "${template}"
		/bin/chmod go+rx "${template}"
		/bin/chmod go+rx "${template}/Public"
		/bin/chmod 733 "${template}/Public/Drop Box"
		/bin/chmod go+rx "${template}/Sites"
	else
		echo "There was an error copying ${denisonmacHome} to ${template}...bailing out."
		exit 2
	fi

	echo "Default template has been updated."
else
	echo "${denisonmacHome} doesn't exist - cannot update template."
fi

exit 0
